//
//  FileData.m
//  iLiteNote
//
//  Created by xiangism on 15/8/26.
//  Copyright (c) 2015年 LiteNice. All rights reserved.
//

#import "FileData.h"

@implementation FileData

- (id)initWithName:(NSString *)path isFile:(BOOL)isFile children:(NSMutableArray *)children
{
    self = [super init];
    if (self) {
        self.children = [NSMutableArray arrayWithArray:children];
        self.path = path;
        self.isFile = isFile;
    }
    return self;
}

+ (id)dataObjectWithName:(NSString*)path isFile:(BOOL)isFile children:(NSMutableArray *)children
{
    return [[self alloc]initWithName:path isFile:isFile children:children];
}

- (void)addChild:(id)child
{
    [self.children insertObject:child atIndex:0];
}

- (void)removeChild:(id)child
{
    [self.children removeObject:child];
}

/*- (NSString*)getBaseName
{
    return self.path.lastPathComponent;
}*/

@end


@implementation RATreeView (FileDataSupport)

- (BOOL)expandItem:(NSString*)path
{
    NSArray *arr = [self topChildren];
    FileData *data = nil;
    for (int i = 0; i < arr.count; ++i) {
        data = arr[i];
        
        NSRange r = [path rangeOfString:data.path];
        if (r.length != 0) {
            [self expandRowForItem:data];
            break;
        }
    }
    for (;;) {
        NSArray *arr = [self allChildren:data];
        BOOL haveFind = NO;
        for (int i = 0; i < arr.count; ++i) {
            FileData *item = arr[i];
            
            if (path == item.path) {
                [self expandRowForItem:item];
                return YES;
            } else {
                NSRange r = [path rangeOfString:item.path];
                if (r.length != 0) {
                    data = item;
                    [self expandRowForItem:item];
                    haveFind = YES;
                    break;
                }
                
            }
        } // end for arr
        if (!haveFind) {
            return NO;
        }
    }
    return YES;
}

@end
